import TJavaLib.TList;
import TJavaLib.TPoint;

class TCacheEntry {
	String sAddr;
	long pkID;
}

public class TNode extends TObject {
	public static final byte SENSOR = 1;
	public static final byte GATEWAY = 2;
	
	private byte nodeType;
	private TList pkCache;
		
	protected TPoint loc;
	protected TRadio radio;
	protected TSenInf seninf;
	protected TList nbAddrs, nbCoors; //Neighbor table
	
	public TNode(byte nodeType) {
		super(TObject.NODE);
		this.nodeType = nodeType;
		radio = new TRadio();
		seninf = new TSenInf();
		pkCache = new TList(1000);
		nbAddrs = new TList();
		nbCoors = new TList();
	}
	
	public byte getNodeType() { return nodeType; }
	public TRadio getRadio() { return radio; }
	public TSenInf getSenInf() { return seninf; }
	public TPoint getLoc() { return loc; }
	public void setLoc(TPoint loc) { this.loc = loc; }
	public void setLoc(double x, double y) { this.loc = new TPoint(x, y); }
	
	public void invokeBootup() { 
		switch (nodeType) {
			case TNode.SENSOR:
				((TSensor)this).bootup();
				break;
			
			case TNode.GATEWAY:
				((TGateway)this).bootup();
				break;
				
			default: break;
		}
	}
	
	public void invokeCallback(byte taskName) { 
		switch (nodeType) {
			case TNode.SENSOR:
				((TSensor)this).callback(taskName);
				break;
			
			case TNode.GATEWAY:
				((TGateway)this).callback(taskName);
				break;
				
			default: break;
		}
	}
	
	//----------------------------For simulation Setup-----------------------------
	public String getAddr() {
		return radio.getAddr();
	}
	
	public void cache(TPacket pk) {
		pkCache.enQueue(pk);
	}
	
	public void send(TPacket packet) {
		TJob job = new TJob(TJob.SEND, TTask.NONE, packet, this, null);
		simulator.addJob(job);
	}
	
	public void execute(TJob job) {
		switch (job.getJobType()) {
			case TJob.RECEIVE:
				if (nodeType==TNode.SENSOR)
					((TSensor)this).processPacket(job.getPacket());
				else if (nodeType==TNode.GATEWAY)
					((TGateway)this).processPacket(job.getPacket());
				break;
				
			default: break;
		}
	}
}
